#include "stdafx.h"
#include "ILuaCallback.h"
#include "config/Def.h"
#include "LuaScript.h"

using namespace unigame;

ILuaCallback::ILuaCallback(const char* name)
{
	int32u len = strlen(name) + 1;
	m_name = uni_new char[len];
	memcpy(m_name, name, len);
}

ILuaCallback::~ILuaCallback()
{
	for(std::map<lua_State*, LuaScript*>::iterator it = m_scripts.begin(); it != m_scripts.end(); ++it)
	{
		it->second->removeCallback(m_name);
	}
	m_scripts.clear();
	uni_delete_array(m_name);
}

void ILuaCallback::addScript(LuaScript* s)
{
	std::map<lua_State*, LuaScript*>::iterator it = m_scripts.find(s->state());
	if(it == m_scripts.end())
	{
		m_scripts[s->state()] = s;
	}
}

void ILuaCallback::removeScript(LuaScript* s)
{
	std::map<lua_State*, LuaScript*>::iterator it = m_scripts.find(s->state());
	if(it != m_scripts.end())
	{
		m_scripts.erase(it);
	}
}